package cn.smile.utils;

import org.springframework.expression.EvaluationContext;
import org.springframework.expression.spel.support.StandardEvaluationContext;

import java.util.HashMap;
import java.util.LinkedHashMap;

/**
 * @author smile-jt
 */
public class EntityUtil {

    /**
     * Object对象转EvaluationContext
     *
     * @param obj object对象
     * @return 转换结果
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public static EvaluationContext objectToContext(Object obj) {
        EvaluationContext context = new StandardEvaluationContext(obj);
        if (obj instanceof LinkedHashMap map) {
            for (Object key : map.keySet()) {
                context.setVariable(key.toString(), map.getOrDefault(key, ""));
            }
        } else if (obj instanceof HashMap hashMap) {
            for (Object key : hashMap.keySet()) {
                context.setVariable(key.toString(), hashMap.get(key));
            }
        }
        return context;
    }
}
